<?
    function getRootFinding($xml_file) {
        global $equation;
        //$objeto  = domxml_open_file($xml_file);
        //$raiz = $objeto->document_element();
        $objeto = new DOMDocument();
        $objeto->load($xml_file);
        $filho = $objeto->getelementsbytagname("rootfinding");
        if ($filho->length > 0) {
            $equation = $filho->item(0)->getelementsbytagname("equation");
            return $equation;
        }
        else return null;
    }

    function getMaxIter($indice) {
        global $equation, $maxiter;
        $mmm = $equation->item($indice)->getelementsbytagname("maxiter");
        if ($mmm->length > 0) {
            $maxiter = $mmm->item(0)->nodeValue;
            return $maxiter;
        }
        else return null;
    }

    function getError($indice) {
        global $equation, $err;
        $eee = $equation->item($indice)->getelementsbytagname("error");
        if ($eee->length > 0) {
            $err = $eee->item(0)->nodeValue;
            return $err;
        }
        else return null;
    }

    function getDegree($indice) {
        global $equation, $degree;
        $dg = $equation->item($indice)->getelementsbytagname("degree");
        if ($dg->length > 0) {
            $degree = $dg->item(0)->nodeValue;
            return $degree;
        }
        else return null;
    }

    function getFunction($indice) {
        global $equation, $f;
        $function = $equation->item($indice)->getelementsbytagname("function");
        if ($function->length > 0) {
            $f = $function->item(0)->nodeValue;
            return $f;
        }
        else return null;
    }

    function getData($indice) {
        global $equation, $data;
        $option = $equation->item($indice)->getelementsbytagname("option");
        if ($option->length > 0) {
            $data = $option->item(0)->getattribute("data");
            return $data;
        }
        else return null;
    }

    function getCoef($indice) {
        global $equation, $cf;
        $coefic = $equation->item($indice)->getelementsbytagname("coef");
        if ($coefic->length > 0) {
            for($i = 0; $i < $coefic->length; $i++) {
                $cf[$i] = $coefic->item($i)->nodeValue;
            }
            return $cf;
        }
        else return null;
    }

    function getGraphInterval($indice)
    {
        global $equation, $graph;
        $interval = $equation->item($indice)->getelementsbytagname("graph_interval");
        if ($interval->length > 0) {
            $x = $interval->item(0)->getelementsbytagname("x1");
            $graph[0] = $x->item(0)->nodeValue;
            $y = $interval->item(0)->getelementsbytagname("x2");
            $graph[1] = $y->item(0)->nodeValue;
            return $graph;
        }
        else return null;
    }

    function getIVector($indice) {
        global $equation, $ivector;
        $ivvv = $equation->item($indice)->getelementsbytagname("vector_initial");
        if ($ivvv->length > 0) {
                $x = $ivvv->item(0)->getelementsbytagname("x");
                for($j = 0; $j < $x->length; $j++) {
                $ivector[$j] = $x->item($j)->nodeValue;
            }
            return $ivector;
        }
        else return null;
    }

    function getDesc($indice) {
        global $equation, $desc;
        $descricao = $equation->item($indice)->getelementsbytagname("description");
        $desc = $descricao->item(0)->nodeValue;
        return $desc;
    }

    function getCom($indice) {
        global $equation, $com;
        $comentario = $equation->item($indice)->getelementsbytagname("comments");
        if ($comentario->length > 0) {
            $com = $comentario->item(0)->nodeValue;
            return $com;
        }
        else return null;
    }

    function getRef($indice) {
        global $equation, $ref;
        $referencia = $equation->item($indice)->getelementsbytagname("reference");
        if ($referencia->length > 0) {
            $ref = $referencia->item(0)->nodeValue;
            return $ref;
        }
        else return null;
    }

    function createMenu() {
        global $equation,$msg;
        $temp1=0;
        for($i = 0; $i < $equation->length; $i++) {
            $temp = getData($i);
            if ($temp=="polynom") {
                $temp1++;
                echo "<a class=bibliotecaexemplos href=javascript:createLinks($i)><u>";
                echo $msg->get('XML_EXIBE_EQNL_MSG01')." "."$temp1".$msg->get('XML_EXIBE_EQNL_MSG02');
                $temp = $msg->get('XML_EXIBE_EQNL_MSG03');
                $temp .= " (Grau ".getDegree($i).")";
                echo "$temp";
                echo "</u></a><p>";

            }
        }
        for($i = 0; $i < $equation->length; $i++) {
            $temp = getData($i);
            if ($temp=='function') {
                $temp1++;
                echo "<a class=bibliotecaexemplos href=javascript:createLinks($i)><u>";
                echo $msg->get('XML_EXIBE_EQNL_MSG01')." "."$temp1".$msg->get('XML_EXIBE_EQNL_MSG02');
                $temp = $msg->get('XML_EXIBE_EQNL_MSG04');
                echo "$temp";
                echo "</u></a><p>";
            }
        }
        return;
    }

    if (empty($xml_file) && empty($id_bib)) $id_bib="bib_demo.xml";
    if ( (strlen(@$indice)==0) || ($indice == null) ) { ?>
<p>

        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function createLinks(value) {
               document.myform.indice.value = value;
               document.myform.action = 'index.php?inc=eqnl1.php&modo=exemplo';
               document.myform.submit();
            }

        </script>


        <? if (isset($id_bib)) $xml_file = $DOCUMENT_ROOT."/bibliotecas/".$id_bib; ?>
        <? verificaBib(@$xml_file); ?>
        <?  if (file_exists($xml_file)) {
                $temp = getRootFinding($xml_file);
                if (!$temp==null) {
                   echo "<p>";
                   echo "<p align=left><font face=Arial>".$msg->get('XML_EXIBE_MSG04')."</font>";
                   echo "<p>";
                   createMenu();
                } else {
                     echo $msg->get('XML_EXIBE_MSG05');
                     echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
                }
        } else {
                 echo $msg->get('XML_EXIBE_MSG06');
                 echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
        }
 	 ?>

        <input type="hidden" name="id_bib" value="<? echo $id_bib ?>">
        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="indice">
        <input class="button" type="button" value="<?php echo $msg->get('XML_EXIBE_MSG07') ?>" onClick="history.back()">
        </form>

<? } else { ?>
<p>

        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function sendToCalculus() {

               document.myform.action = 'index.php?id_bib=<?echo $id_bib?>&indice=<?echo $indice?>&inc=eqnl1.php&modo=exemplo';
               document.myform.submit();
            }

        </script>

        <? getRootFinding($xml_file); ?>
        <? $dados = getData($indice);?>
        <? $funcao = getFunction($indice);?>
        <? $deg = getDegree($indice);?>
        <? $coef = getCoef($indice);?>
        <? $error = getError($indice);?>
        <? $it = getMaxIter($indice);?>
        <? $ivct = getIVector($indice);?>
        <? $graph = getGraphInterval($indice);?>
        <? if (!$ivct==null) {
             $x0 = @$ivct[0];
             $x1 = @$ivct[1];
           } ?>
        <? $desc = getDesc($indice); ?>
        <? $com = getCom($indice); ?>
        <? $ref = getRef($indice); ?>
        <? verificaBib(@$xml_file); ?>

        <? if (!strlen($desc) == 0) { ?>
              <p align="left"><font face="Arial"><b> <? echo $msg->get('XML_EXIBE_EQNL_MSG10'); ?> </b><? echo htmlentities(utf8_decode($desc)) ?></font>
        <? } ?>
        
        <? if ($dados=='function') { ?>
        <input type="hidden" name="opcao_execucao" value="1">
	    <p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_EQNL_MSG05'); ?></font>
		<p align="left"><font face="Arial">f(x) = </font>
        <input type="hidden" size='60' maxlength='100' name="funcao" value="<? echo $funcao ?>"><? echo $funcao ?>
		<p>
        <? } else if ($dados=='polynom') { ?>
          <input type="hidden" name="opcao_execucao" value="0">
		  <p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_EQNL_MSG06'); ?> <? echo $deg ?></font>
		  <input type="hidden" name="deg" value="<? echo $deg ?>">
		  <p>
		  <p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_EQNL_MSG07'); ?> </font>
		  <p>
          <p align="left"><font face="Arial">f(x) = </font>
          <? for($i=0;$i<$deg;$i++) { ?>
               <input type=hidden size=5 maxlength=10 name=coef[<? echo $i ?>] value="<? echo $coef[$i] ?>"> <? echo $coef[$i] ?>x <sup><? echo ($deg-$i) ?></sup> +
          <? } ?>
       	  <input type=hidden size=5 maxlength=10 name=coef[<? echo $deg ?>] value="<? echo $coef[$deg] ?>"><? echo $coef[$deg] ?>
	      <p>
        <? } ?>
		<p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_EQNL_MSG08'); ?> </font>
		<input type=text size=5 maxlength=10 name=error value="<? echo $error ?>">
		<p>
		<p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_EQNL_MSG09'); ?> </font>
		<input type=text size=5 maxlength=10 name=it value="<? echo $it?>">
		<p>
        <? if(!empty($x1)) { ?>
	   	     <input type=hidden size=5 maxlength=5 name=x0 value="<? echo $x0 ?>">
		     <input type=hidden size=5 maxlength=5 name=x1 value="<? echo $x1 ?>">
        <? } ?>
        <input type="hidden" name="graphx1" value="<? echo @$graph[0] ?>">
        <input type="hidden" name="graphx2" value="<? echo @$graph[1] ?>">
        <? if (!$com == null) { ?>
              <p align="left"><font face="Arial"><b> <? echo $msg->get('XML_EXIBE_MSG08'); ?> </b><? echo htmlentities(utf8_decode($com)) ?></font>
        <? } ?>
        <p>
        <? if (!$ref == null) { ?>
              <p align="left"><font face="Arial"><b> <? echo $msg->get('XML_EXIBE_MSG09'); ?> </b><? echo htmlentities(utf8_decode($ref)) ?></font>
        <? } ?>
        <p>

        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="executar" value="sim">
	<input type="hidden" name="navegacao" value="sim">
        <input class="button" type="button" value=<? echo $msg->get('XML_NEXT'); ?> onClick="sendToCalculus()"><p>
        </form>
	<?
	echo "<form name=\"exemplos\" enctype=\"multipart/form-data\" method=\"post\">";
	echo "<script language=\"javascript\" type=\"text/javascript\">\n";
	echo "function criarLinks(value) {\n";
	echo "document.exemplos.indice.value = value;\n";
	echo "document.exemplos.action = 'index.php?inc=eqnl1.php&modo=exemplo'\n";
	echo "document.exemplos.submit();\n";
	echo "}\n";
	echo "</script>";
	echo "<input type=\"hidden\" name=\"id_bib\" value=\"".$id_bib."\">";
	echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
	echo "<input type=\"hidden\" name=\"indice\">";
	echo "</form>";
	echo $msg->get('Outros_Exemplos').":<br>";
	//$objeto  = domxml_open_file($xml_file);
	//$raiz = $objeto->document_element();
    $objeto = new DOMDocument();
    $objeto->load($xml_file);
	$filho = $objeto->getelementsbytagname("rootfinding");
	if ($filho->length > 0)
	{
		$tmp=0;
		$equation = $filho->item(0)->getelementsbytagname("equation");
		for ($i = 0; $i < $equation->length; $i++)
		{
			$temp=$equation->item($i)->getelementsbytagname("option");
			$temp1=$temp->item(0)->getattribute("data");
			if ($temp1 == 'polynom')
			{
			$tmp++;
			if ($i) echo " | ";
			if ($i == $indice) echo "<b>";
			echo "<a href=\"javascript:criarLinks($i);\"><u>".($tmp)."</u></a>";
			if ($i == $indice) echo "</b>";
			}
		}
		for ($i = 0; $i < $equation->length; $i++)
		{
			$temp=$equation->item($i)->getelementsbytagname("option");
			$temp1=$temp->item(0)->getattribute("data");
			if ($temp1 == 'function')
			{
			$tmp++;
			if ($i) echo " | ";
			if ($i == $indice) echo "<b>";
			echo "<a href=\"javascript:criarLinks($i);\"><u>".($tmp)."</u></a>";
			if ($i == $indice) echo "</b>";
			}
		}

	}
	?>

<? } ?>
